import * as apis from '../services/mechanismOperationService.js'
import { routerRedux } from 'dva/router';

export default {
    namespace: 'mechanismOperation',

    state: {
        verificationTechnologyList: [],
        testTechnologyList: [],
        certificationFailure: {
            isFailure: false,
            reason: ''
        },
        mechanism: {}
    },

    reducers: {
        setMechanism: setMechanism,
        setVerificationTechnologyList: setVerificationTechnologyList,
        setTestTechnologyList: setTestTechnologyList
    },

    effects: {
        getVerificationTechnologyTableDataByMechanismId: getVerificationTechnologyTableDataByMechanismId,
        getTestTechnologyTableDataByMechanismId: getTestTechnologyTableDataByMechanismId,
        getMechanismByMechanismId: getMechanismByMechanismId,
        uploadMechanismInformation: uploadMechanismInformation,
    }
}

function setTestTechnologyList(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.testTechnologyList = payload.data;
    return _state
}

function setVerificationTechnologyList(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.verificationTechnologyList = payload.data;
    return _state
}

function setMechanism(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.mechanism = payload.data
    _state.mechanism.examinationStatus = payload.data.MechanismExamination.examinationStatus
    _state.mechanism.reason = payload.data.MechanismExamination.reason
    return _state
}

/**
 * 获取测试机构所要操作的所有技术列表
 */
function* getTestTechnologyTableDataByMechanismId(payload, { select, call, put }) {
    let result = yield call(apis.getTestTechnologyTableDataByMechanismId, payload.data)
    for (let i = 0; i < result.data.data.length; i++) {
        for (let key in result.data.data[i]) {
            if (key == "TestFile") {
                for (let key2 in result.data.data[i][key]) {
                    if (key2 == "testAgreement") {
                        result.data.data[i].testAgreement = result.data.data[i][key][key2]
                    }

                    if (key2 == "testReport") {
                        result.data.data[i].testReport = result.data.data[i][key][key2]
                    }

                    if (key2 == "testData") {
                        result.data.data[i].testData = result.data.data[i][key][key2]
                    }

                }
            }

            if (key == "createdAt") {
                result.data.data[i].createdAt = result.data.data[i].createdAt.substring(0, 10)
            }
        }
    }

    yield put({
        type: "mechanismOperation/setTestTechnologyList",
        data: result.data.data
    })
}

/**
 * 获取验证机构所要操作的所有技术列表 
 */
function* getVerificationTechnologyTableDataByMechanismId(payload, { select, call, put }) {
    let result = yield call(apis.getVerificationTechnologyTableDataByMechanismId, payload.data)
    for (let i = 0; i < result.data.data.length; i++) {
        for (let key in result.data.data[i]) {
            if (key == "VerificationFile") {
                for (let key2 in result.data.data[i][key]) {
                    if (key2 == "verificationAgreement") {
                        result.data.data[i].verificationAgreement = result.data.data[i][key][key2]
                    }

                    if (key2 == "verificationReport") {
                        result.data.data[i].verificationReport = result.data.data[i][key][key2]
                    }

                    if (key2 == "verificationStatement") {
                        result.data.data[i].verificationStatement = result.data.data[i][key][key2]
                    }

                    if (key2 == "dataauditReport") {
                        result.data.data[i].dataauditReport = result.data.data[i][key][key2]
                    }

                    if (key2 == "licenseAgreement") {
                        result.data.data[i].licenseAgreement = result.data.data[i][key][key2]
                    }
                }
            }

            if (key == "createdAt") {
                result.data.data[i].createdAt = result.data.data[i].createdAt.substring(0, 10)
            }
        }
    }

    yield put({
        type: "mechanismOperation/setVerificationTechnologyList",
        data: result.data.data
    })
}

/**
 * 根据用户id获取绑定的机构信息
 */
function* getMechanismByMechanismId(payload, { select, call, put }) {
    let result = yield call(apis.getMechanismByMechanismId, payload.data)

    yield put({
        type: "mechanismOperation/setMechanism",
        data: result.data.data
    })

}

/**
 * 上传机构信息
 */
function* uploadMechanismInformation(payload, { select, call, put }) {
    let result = yield call(apis.uploadMechanismInformation, payload.data)

    yield put({
        type: 'app/setUserMechanismId',
        data: result.data.data
    })

    yield put(routerRedux.replace('/'))

    yield put({
        type: 'app/setServerRes',
        data: result.data
    })

    yield put({
        type: 'app/initServerRes',
    })
}